import axios from "axios";
import router from "@/router";


// const COMMON_SUCCESS = 'success';
// const COMMON_ERROR = 'error';
// const COMMON_USER_INVALID = 'invalidPrincipal';

// 超时时间
axios.defaults.timeout = 60000;

axios.defaults.baseURL = 'http://zcapi.hrgsxt.cn/';
axios.interceptors.request.use(config => {
  //Loading方法
  config.headers['Content-Type'] = "application/x-www-form-urlencoded;charset=UTF-8";
  if (config.method == 'post' || config.method == 'put') {
    config.transformRequest = [function (data) {
      let ret = '';
      for (let i in data) {
        ret += encodeURIComponent(i) + "=" + encodeURIComponent(data[i]) + "&";
      }
      return ret;
    }];
  }
  return config
}, error => {
  // 加载超时
  return Promise.reject(error)
});

// http响应拦截器
axios.interceptors.response.use(data => {
  if (data.data.code === 200) {
    return data.data;
  } else if (data.data.code === 401) {
    return Promise.reject(data.data.message);
  } else if (data.data.code === 400) {
    // localStorage.removeItem('token');
    return Promise.reject(data.data.message);
  } else {
    return data.data;
  }
}, error => {
  if (axios.isCancel(error)) {
    if (error) {
      console.log('请求关闭', error.message);
    }
  } else {
    console.log('error', error);
  }
  return Promise.reject(error);
});


export default axios
